const { ProvidePlugin, DefinePlugin } = require('webpack');
module.exports = {
  // 不需要额外转译的依赖
  transpileDependencies: [],
  pluginOptions: {
    electronBuilder: {
      // 主进程入口文件
      mainProcessFile: 'src/background.js',
      // 渲染进程预加载脚本
      //preload: 'src/preload.js',
      // 渲染进程主入口
      rendererProcessFile: 'src/main.js',
      // 启用 Node.js 集成（开发环境建议开启）
      nodeIntegration: true,
      // 上下文隔离（增强安全性，如需使用 nodeIntegration 建议设为 true）
      contextIsolation: false,
      // 构建输出目录
      outputDir: 'dist_electron',
      // 构建模式
      mode: 'modern',
      // 可选：禁用自动监听主/渲染进程文件变化
      disableMainProcessWatch: true,
      disableRendererProcessWatch: true,
      // 添加主进程的 webpack 配置
      chainWebpackMainProcess: (config) => {
        config.resolve.alias
          .set('node:util', 'util')
          .set('node:path', 'path')
          .set('node:os', 'os')
          .set('node:events', 'events')
          .set('node:stream', 'stream')
          .set('node:buffer', 'buffer');
      },
      builderOptions: {
        asar: true,
        appId: "com.example.softmanagement",
        productName: "SoftManagement",
        win: {
          target: "msi",
          icon: "public/favicon.ico"
        },
        msi: {
          perMachine: false // 👈 true为所有用户安装,false当前用户
        },
        nsis: {
          oneClick: false, // 可自定义 NSIS 安装选项
          allowToChangeInstallationDirectory: true,
          allowElevation: true, // 允许用户权限提升
          perMachine: false,
          createDesktopShortcut: true,
          createStartMenuShortcut: true,
          shortcutName: "Autodesk快捷方式",
          installerIcon: "public/favicon.ico",
          uninstallerIcon: "public/favicon.ico"
        },
        // 👇 添加 extraResources 将 IconExtractor.exe 包含进 resources 目录
        extraResources: [
          {
            from: 'node_modules/icon-extractor/bin/IconExtractor.exe',
            to: 'bin/IconExtractor.exe'
          },
          {
            from: 'node_modules/icon-extractor/bin/Newtonsoft.Json.dll',
            to: 'bin/Newtonsoft.Json.dll'
          },
          {
            from: 'public/favicon.ico',
            to: 'public/favicon.ico'
          }
        ],
        // 确保文件不被打包进 app.asar
        files: [
          "!node_modules/icon-extractor/bin/**/*",
          "!public/**/*"
        ]
      }
    }
  },
  configureWebpack: {
    devtool: false, // 关闭 热部署
    performance: { hints: false }, // 关闭性能提示
    // 解决 Electron 中 Node.js 模块在浏览器端缺失的问题
    resolve: {
      alias: {
        fs: 'browserify-fs',
        path: 'path-browserify',
        stream: 'stream-browserify',
        buffer: 'buffer',
        process: 'process/browser',
        util: 'util',
        vuedraggable: 'vuedraggable',
        'node:events': 'events',
        'node:os': 'os',
        'node:path': 'path',
        'node:stream': 'stream',
        'node:util': 'util'
      },
      fallback: {
        // 适配 Node.js 核心模块
        crypto: require.resolve('crypto-browserify'),
        http: require.resolve('stream-http'),
        https: require.resolve('https-browserify'),
        os: require.resolve('os-browserify/browser'),
        url: require.resolve('url/'),
        util: require.resolve('util'),
        path: require.resolve('path-browserify'),
        events: require.resolve('events'),
        stream: require.resolve('stream-browserify')
      }
    },
    plugins: [
      // 自动注入全局变量
      new ProvidePlugin({
        Buffer: ['buffer', 'Buffer'],
        process: ['process/browser']
      }),
      // 定义 global 别名为 window
      new DefinePlugin({
        global: 'window'
      }),
    ],
    externals: {
      // 避免将原生模块打包进 renderer 进程
      'ffi': 'commonjs ffi',
      'ref': 'commonjs ref',
      'bindings': 'commonjs bindings',
      'win-icon-extractor': 'commonjs win-icon-extractor'
    }
  }
};